package Test1;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 表达式生成器类，负责生成各种类型的算术表达式
 * 支持加减乘除四则运算
 */
public class ExpressionGenerator {
    private Random random;
    private Validator validator;

    // 表达式类型常量
    public static final int PURE_ADDITION_2 = 1;
    public static final int PURE_SUBTRACTION_2 = 2;
    public static final int PURE_MULTIPLICATION_2 = 3;
    public static final int PURE_DIVISION_2 = 4;
    public static final int MIXED_ADD_SUB_2 = 5;
    public static final int MIXED_MUL_DIV_2 = 6;
    public static final int MIXED_4_OPERATIONS_2 = 7;
    public static final int MIXED_4_OPERATIONS_3 = 8;

    public ExpressionGenerator() {
        this.random = new Random();
        this.validator = new Validator();
    }

    /**
     * 生成表达式列表
     */
    public List<Expression> generateExpressions(int type, int count) {
        List<Expression> expressions = new ArrayList<>();
        int attempts = 0;
        int maxAttempts = count * 20; // 防止无限循环

        while (expressions.size() < count && attempts < maxAttempts) {
            try {
                Expression expr = generateSingleExpression(type);
                if (expr != null && validator.isValid(expr)) {
                    expressions.add(expr);
                }
            } catch (Exception e) {
                // 忽略生成过程中的异常，继续尝试
            }
            attempts++;
        }

        return expressions;
    }

    /**
     * 生成单个表达式
     */
    private Expression generateSingleExpression(int type) {
        switch (type) {
            case PURE_ADDITION_2:
                return generateTwoNumberExpression("+");
            case PURE_SUBTRACTION_2:
                return generateTwoNumberExpression("-");
            case PURE_MULTIPLICATION_2:
                return generateTwoNumberExpression("×");
            case PURE_DIVISION_2:
                return generateDivisionExpression();
            case MIXED_ADD_SUB_2:
                return generateTwoNumberExpression(random.nextBoolean() ? "+" : "-");
            case MIXED_MUL_DIV_2:
                return generateMixedMulDivExpression();
            case MIXED_4_OPERATIONS_2:
                return generateMixedFourOperations2();
            case MIXED_4_OPERATIONS_3:
                return generateThreeNumberExpression();
            default:
                return null;
        }
    }

    /**
     * 生成两个数的表达式
     */
    private Expression generateTwoNumberExpression(String operator) {
        int num1 = random.nextInt(98) + 1; // 1-99
        int num2 = random.nextInt(98) + 1; // 1-99

        return new Expression(num1, operator, num2);
    }

    /**
     * 生成除法表达式（确保结果为整数）
     */
    private Expression generateDivisionExpression() {
        int result = random.nextInt(98) + 1; // 1-99
        int divisor = random.nextInt(98) + 1; // 1-99
        int dividend = result * divisor;

        // 确保被除数也在范围内
        while (dividend >= 100 || dividend <= 0) {
            result = random.nextInt(98) + 1;
            divisor = random.nextInt(98) + 1;
            dividend = result * divisor;
        }

        return new Expression(dividend, "÷", divisor);
    }

    /**
     * 生成乘除混合表达式
     */
    private Expression generateMixedMulDivExpression() {
        if (random.nextBoolean()) {
            return generateTwoNumberExpression("×");
        } else {
            return generateDivisionExpression();
        }
    }

    /**
     * 生成四则混合表达式（两个数）
     */
    private Expression generateMixedFourOperations2() {
        String[] operators = {"+", "-", "×", "÷"};
        String operator = operators[random.nextInt(operators.length)];

        if ("÷".equals(operator)) {
            return generateDivisionExpression();
        } else {
            return generateTwoNumberExpression(operator);
        }
    }

    /**
     * 生成三个数的混合表达式
     */
    private Expression generateThreeNumberExpression() {
        int num1 = random.nextInt(98) + 1;
        int num2 = random.nextInt(98) + 1;
        int num3 = random.nextInt(98) + 1;

        String[] operators = {"+", "-", "×", "÷"};
        String op1 = operators[random.nextInt(operators.length)];
        String op2 = operators[random.nextInt(operators.length)];

        return new Expression(num1, op1, num2, op2, num3);
    }

    /**
     * 获取类型名称
     */
    public static String getTypeName(int type) {
        switch (type) {
            case PURE_ADDITION_2: return "纯加法（2个数）";
            case PURE_SUBTRACTION_2: return "纯减法（2个数）";
            case PURE_MULTIPLICATION_2: return "纯乘法（2个数）";
            case PURE_DIVISION_2: return "纯除法（2个数）";
            case MIXED_ADD_SUB_2: return "加减混合（2个数）";
            case MIXED_MUL_DIV_2: return "乘除混合（2个数）";
            case MIXED_4_OPERATIONS_2: return "四则混合（2个数）";
            case MIXED_4_OPERATIONS_3: return "四则混合（3个数）";
            default: return "未知类型";
        }
    }
}